package com.chaoyong.higo.activity;

import java.util.ArrayList;
import java.util.List;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.chaoyong.higo.R;
import com.chaoyong.higo.base.BaseActivity;
import com.chaoyong.higo.cainiaowo.CartProvider;
import com.chaoyong.higo.cainiaowo.ShoppingCart;
import com.chaoyong.higo.fragment.FragAllGoods_z;
import com.chaoyong.higo.fragment.FragHomePage;
import com.chaoyong.higo.fragment.FragHomePage.SetClickListener;
import com.chaoyong.higo.fragment.FragMyPurchase;
import com.chaoyong.higo.fragment.FragShoppingBar2;
import com.chaoyong.higo.fragment.FragZuixinjiexiao;
import com.chaoyong.higo.utils.DoubleClickExitHelper;
import com.chaoyong.higo.utils.V;
import com.chaoyong.higo.utils.Values;
import com.chaoyong.higo.view.BadgeView;
import com.chaoyong.higo.view.MyViewPager;
import com.pgyersdk.update.PgyUpdateManager;

public class MainActivity extends BaseActivity implements OnClickListener, SetClickListener {

	private DoubleClickExitHelper mDoubleClickExit;
	private Context mContext;
	private MyViewPager main_vPager;
	private LinearLayout main_main_lay, main_allgoods_lay, main_newest_lay, main_cart_lay, main_my_lay;
	private ImageView mian_my_img, mian_main_img, mian_allgoods_img, mian_newest_img, mian_cart_img;
	private TextView mian_cart_tx, mian_my_tx, mian_newest_tx, mian_allgoods_tx, mian_main_tx;
	private ArrayList<Fragment> listfreg;
	// 初始化
	private FragHomePage fragHomePage;
	// private FragAllShops fragAllShops;
	private FragAllGoods_z fragAllShops;
	//private FragNewsAnnounced fragNewsAnnounced;
	private FragZuixinjiexiao fragNewsAnnounced;
	// private FragShoppingBar fragShoppingBar;
	private FragShoppingBar2 fragShoppingBar;
	private FragMyPurchase fragMyPurchase;
	
	private int tag = 10;
	
	private ProgressDialog pd;
	
	public static BadgeView badgeView;
	private LinearLayout layout;
	
	private List<ShoppingCart> carts;
	private CartProvider cartProvider;

	//private int success = 10;
	
	// @Override
	// protected void onCreate(Bundle arg0) {
	// super.onCreate(arg0);
	// setContentView();
	// findView();
	// init();
	// }

	public void findView() {
		main_vPager = (MyViewPager) findViewById(R.id.main_vPager);

		layout = V.f(this, R.id.main_cart_lay);
		
		mian_main_img = (ImageView) findViewById(R.id.mian_main_img);
		mian_my_img = (ImageView) findViewById(R.id.mian_my_img);
		mian_cart_img = (ImageView) findViewById(R.id.mian_cart_img);
		mian_allgoods_img = (ImageView) findViewById(R.id.mian_allgoods_img);
		mian_newest_img = (ImageView) findViewById(R.id.mian_newest_img);

		mian_main_tx = (TextView) findViewById(R.id.mian_main_tx);
		mian_my_tx = (TextView) findViewById(R.id.mian_my_tx);
		mian_newest_tx = (TextView) findViewById(R.id.mian_newest_tx);
		mian_allgoods_tx = (TextView) findViewById(R.id.mian_allgoods_tx);
		mian_cart_tx = (TextView) findViewById(R.id.mian_cart_tx);

		main_main_lay = (LinearLayout) findViewById(R.id.main_main_lay);
		main_allgoods_lay = (LinearLayout) findViewById(R.id.main_allgoods_lay);
		main_newest_lay = (LinearLayout) findViewById(R.id.main_newest_lay);
		main_cart_lay = (LinearLayout) findViewById(R.id.main_cart_lay);
		main_my_lay = (LinearLayout) findViewById(R.id.main_my_lay);

		main_main_lay.setOnClickListener(this);
		main_allgoods_lay.setOnClickListener(this);
		main_newest_lay.setOnClickListener(this);
		main_cart_lay.setOnClickListener(this);
		main_my_lay.setOnClickListener(this);
		
		
		
		
		//PgyUpdateManager.register(this);
		
	}
	

	@Override
	protected void onNewIntent(Intent intent) {
		// TODO Auto-generated method stub
		super.onNewIntent(intent);
		tag = intent.getIntExtra("tag", 11);
		//showToast(tag + "");
		switch (tag) {
		case 0:
			BgChange(0);
			break;
		case 1:
			BgChange(1);
			break;
		case 2:
			BgChange(2);
			break;
		case 3:
			BgChange(3);
			break;
		case 4:
			BgChange(4);
			break;
		default:
			BgChange(0);
			break;
		}
	}
	
	/*protected void onStartt() {
		// TODO Auto-generated method stub
		super.onStart();
		tag = getIntent().getIntExtra("tag", 10);
		showToast(tag + "");
		
		switch (tag) {
		case 0:
			BgChange(0);
			break;
		case 1:
			BgChange(1);
			break;
		case 2:
			BgChange(2);
			break;
		case 3:
			BgChange(3);
			break;
		case 4:
			BgChange(4);
			break;
		default:
			BgChange(0);
			break;
		}
	}*/
	
	public void init() {
		mContext = this;
		
		cartProvider = new CartProvider(this);
		carts = cartProvider.getAll();
		
		
		
		badgeView = new BadgeView(mContext, layout);
		badgeView.setBadgeMargin(30, 0);
		//badgeView.setBadgeCount(Values.scno);
		if(carts != null){
			badgeView.setBadgeCount(carts.size());
		}else{
			badgeView.setBadgeCount(0);
		}
		
		badgeView.show();
		
		mDoubleClickExit = new DoubleClickExitHelper(this);
		fragHomePage = new FragHomePage();
		fragAllShops = new FragAllGoods_z(badgeView);
		fragNewsAnnounced = new FragZuixinjiexiao();
		// fragShoppingBar = new FragShoppingBar();
		fragShoppingBar = new FragShoppingBar2(badgeView);
		fragMyPurchase = new FragMyPurchase();

		listfreg = new ArrayList<Fragment>();
		listfreg.add(fragHomePage);
		listfreg.add(fragAllShops);
		listfreg.add(fragNewsAnnounced);
		listfreg.add(fragShoppingBar);
		listfreg.add(fragMyPurchase);

		main_vPager.setAdapter(new MyPagerAdapter(getSupportFragmentManager()));
		main_vPager.setOnPageChangeListener(new MyOnPageChangeListener());

		//onStartt();
		
		
	}

	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			return mDoubleClickExit.onKeyDown(keyCode, event,this);
		}
		return super.onKeyDown(keyCode, event);
	}
	
	/*@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		if (keyCode == KeyEvent.KEYCODE_BACK) {  
            moveTaskToBack(false);  
            return true;  
        }  
        return super.onKeyDown(keyCode, event);  
	}*/
	
	
	/*private long exitTime = 0; 
	
	@Override  
	public boolean onKeyDown(int keyCode, KeyEvent event) {  
	    if(keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN){     
	        if((System.currentTimeMillis()-exitTime) > 2000){    
	            Toast.makeText(getApplicationContext(), "再按一次退出程序", Toast.LENGTH_SHORT).show();                                  
	            exitTime = System.currentTimeMillis();     
	        } else {  
	            //finish();  
	            //System.exit(0); 
	        	moveTaskToBack(false); 
	        }  
	        return true;     
	    }  
	    return super.onKeyDown(keyCode, event);  
	}  */

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.main_main_lay:
			BgChange(0);
			break;
		case R.id.main_allgoods_lay:
			BgChange(1);
			break;
		case R.id.main_newest_lay:
			BgChange(2);
			break;
		case R.id.main_cart_lay:
			BgChange(3);
			break;
		case R.id.main_my_lay:
			BgChange(4);
			break;

		default:
			break;
		}
	}

	class MyPagerAdapter extends FragmentPagerAdapter {

		public MyPagerAdapter(FragmentManager fm) {
			super(fm);
		}

		@Override
		public int getCount() {
			return listfreg.size();
		}

		@Override
		public Fragment getItem(int arg0) {
			return listfreg.get(arg0);
		}

	}

	class MyOnPageChangeListener implements OnPageChangeListener {

		@Override
		public void onPageScrollStateChanged(int arg0) {
		}

		@Override
		public void onPageScrolled(int arg0, float arg1, int arg2) {
		}

		@Override
		public void onPageSelected(int arg0) {
			BgChange(arg0);
		}

	}

	private void BgChange(int tag) {
		if (tag == 0) {
			mian_main_img.setImageResource(R.drawable.home_selected);
			mian_main_tx.setTextColor(mContext.getResources().getColor(R.color.red_title));

			mian_allgoods_img.setImageResource(R.drawable.allgoods);
			mian_allgoods_tx.setTextColor(mContext.getResources().getColor(R.color.main_fregment));

			mian_newest_img.setImageResource(R.drawable.newest);
			mian_newest_tx.setTextColor(mContext.getResources().getColor(R.color.main_fregment));

			mian_cart_img.setImageResource(R.drawable.cart);
			mian_cart_tx.setTextColor(mContext.getResources().getColor(R.color.main_fregment));

			mian_my_img.setImageResource(R.drawable.me);
			mian_my_tx.setTextColor(mContext.getResources().getColor(R.color.main_fregment));
			main_vPager.setCurrentItem(0);
		} else if (tag == 1) {
			mian_main_img.setImageResource(R.drawable.home);
			mian_main_tx.setTextColor(mContext.getResources().getColor(R.color.main_fregment));

			mian_allgoods_img.setImageResource(R.drawable.allgoods_select);
			mian_allgoods_tx.setTextColor(mContext.getResources().getColor(R.color.red_title));

			mian_newest_img.setImageResource(R.drawable.newest);
			mian_newest_tx.setTextColor(mContext.getResources().getColor(R.color.main_fregment));

			mian_cart_img.setImageResource(R.drawable.cart);
			mian_cart_tx.setTextColor(mContext.getResources().getColor(R.color.main_fregment));

			mian_my_img.setImageResource(R.drawable.me);
			mian_my_tx.setTextColor(mContext.getResources().getColor(R.color.main_fregment));
			main_vPager.setCurrentItem(1);
		} else if (tag == 2) {
			mian_main_img.setImageResource(R.drawable.home);
			mian_main_tx.setTextColor(mContext.getResources().getColor(R.color.main_fregment));

			mian_allgoods_img.setImageResource(R.drawable.allgoods);
			mian_allgoods_tx.setTextColor(mContext.getResources().getColor(R.color.main_fregment));

			mian_newest_img.setImageResource(R.drawable.newest_select);
			mian_newest_tx.setTextColor(mContext.getResources().getColor(R.color.red_title));

			mian_cart_img.setImageResource(R.drawable.cart);
			mian_cart_tx.setTextColor(mContext.getResources().getColor(R.color.main_fregment));

			mian_my_img.setImageResource(R.drawable.me);
			mian_my_tx.setTextColor(mContext.getResources().getColor(R.color.main_fregment));
			main_vPager.setCurrentItem(2);

		} else if (tag == 3) {
			mian_main_img.setImageResource(R.drawable.home);
			mian_main_tx.setTextColor(mContext.getResources().getColor(R.color.main_fregment));

			mian_allgoods_img.setImageResource(R.drawable.allgoods);
			mian_allgoods_tx.setTextColor(mContext.getResources().getColor(R.color.main_fregment));

			mian_newest_img.setImageResource(R.drawable.newest);
			mian_newest_tx.setTextColor(mContext.getResources().getColor(R.color.main_fregment));

			mian_cart_img.setImageResource(R.drawable.cart_selectt);
			mian_cart_tx.setTextColor(mContext.getResources().getColor(R.color.red_title));

			mian_my_img.setImageResource(R.drawable.me);
			mian_my_tx.setTextColor(mContext.getResources().getColor(R.color.main_fregment));
			main_vPager.setCurrentItem(3);

		} else if (tag == 4) {
			mian_main_img.setImageResource(R.drawable.home);
			mian_main_tx.setTextColor(mContext.getResources().getColor(R.color.main_fregment));

			mian_allgoods_img.setImageResource(R.drawable.allgoods);
			mian_allgoods_tx.setTextColor(mContext.getResources().getColor(R.color.main_fregment));

			mian_newest_img.setImageResource(R.drawable.newest);
			mian_newest_tx.setTextColor(mContext.getResources().getColor(R.color.main_fregment));

			mian_cart_img.setImageResource(R.drawable.cart);
			mian_cart_tx.setTextColor(mContext.getResources().getColor(R.color.main_fregment));

			mian_my_img.setImageResource(R.drawable.me_select);
			mian_my_tx.setTextColor(mContext.getResources().getColor(R.color.red_title));
			main_vPager.setCurrentItem(4);

		}
	}

	/**
	 * 更换tab背景，这个实现方法太菜，更好的方法应该使用selecter
	 * 
	 * @param iv
	 *            要换背景的imageview
	 * @param drawable
	 *            图片资源
	 * @param tv
	 *            要换字体颜色的textView
	 * @param color
	 *            颜色资源
	 */
	private void setDiffentBg(ImageView iv, int drawable, TextView tv, int color) {
		iv.setImageResource(drawable);
		tv.setTextColor(mContext.getResources().getColor(color));
	}

	@Override
	public int layoutResID() {
		return R.layout.activity_main;
	}

	@Override
	public void onNewClick() {
		BgChange(2);
	}

	@Override
	public void onShopsClick() {
		BgChange(1);
	}

	@Override
	public void onNIClick() {

	}
	
	@Override
	public void onUser() {
		// TODO Auto-generated method stub
		BgChange(4);
	}
	
	
	/*@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		//Toast.makeText(mContext, "=========", 0).show();
		if (resultCode == 0) {  
			//Toast.makeText(mContext, "resultCode = " + resultCode, 0).show();
           // mAdapter.delAllCart();
			success = 1;
        }  
         
	}*/
	
	/*@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		for (int i = 0; i < carts.size(); i++) {
			cartProvider.delete(carts.get(i));
		}
		MainActivity.badgeView.decrement(Values.idList.size());
		Values.idList.clear();
	}*/
	
	

}
